/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.kaikeba.co

 */
package com.jshop.seckill.domain;

import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * 购物车
 */
@Data
public class StoreCart implements Serializable {

    //商品数量
    private Integer cartNum;//null int == 0,如果是引用数据类型null
    //商品总价
    private BigDecimal cartTotal;
    //购物项的LinkedHashMap列表
    private LinkedHashMap<String, StoreCartItem> map = new LinkedHashMap<String, StoreCartItem>();

    //购物车中商品的数量 :
    public Integer getCartNum() {
        //设置商品数量默认值，不设置会报空指针
        cartNum = 0;

        //遍历map集合的value值，获取购物项个数，累加得出商品数量
        Set<String> keys = map.keySet();
        for (String key : keys) {
            StoreCartItem cartItem = map.get(key);
            cartNum += cartItem.getCartNum();//累加得出商品数量
            //cartNum = cartNum + cartItem.getNum();
        }
        return cartNum;
    }

    //购物车中商品的总价
    public BigDecimal getCartTotal() {
        //设置默认为0.0
        cartTotal = new BigDecimal(0);

        //2.遍历夫妻对集合，求商品的总价
        for (Map.Entry<String, StoreCartItem> entry : map.entrySet()) {
            StoreCartItem cartItem = entry.getValue();
            //获取商品小计
            BigDecimal subTotal = cartItem.getSubTotal();
            cartTotal.add(subTotal);//累加商品小计
        }
        return cartTotal;
    }
}
